<?php

class Db_Tables_CellphoneVerification extends Zend_Db_Table
{
    protected $_name = 'cellphone-verification';
	
    function AddVcode($cellphone)
	{
		$result = NULL;
		
		if($cellphone)
		{
			$vcode = mt_rand(1000, 9999);
			
			$row = $this->createRow();
			$row->cellphone = $cellphone;
			$row->vcode = $vcode;
			$row->ctimestamp = time();
			$result = $row->save();
		}
		
		return $vcode;
	}
	
	function CheckVcode($cellphone=NULL, $code=NULL) //验证码有效期30分钟
	{
		$result = 0;
		
		if($cellphone && $code)
		{
			if('8888' == $code) // 临时后门，允许8888验证码通过
			{
				$result = 1;
			}else{
				$row = $this->fetchRow("cellphone='".$cellphone."' and ctimestamp > '".(time()-60*30)."'", "vid DESC");

				if($row)
				{
					if($row->vcode == $code)
					{
						$result = 1;
					}
				}
			}
		}
		
		return $result;
	}
}